/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unitn.science.matr155863_148346_145963.bccastestore.utils;


import it.unitn.science.matr155863_148346_145963.bccastestore.items.User;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author berto
 */
public class UserDetailsValidator {
    
          
    
    private static Date dateFromString(String string) {
        try {
            SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy",Locale.ITALIAN);
            sf.setLenient(false);
            Date date=sf.parse(string);
            Calendar today = Calendar.getInstance();
            today.clear(Calendar.HOUR); today.clear(Calendar.MINUTE); today.clear(Calendar.SECOND);
            Date todayDate = today.getTime();
            if(date.compareTo(todayDate)>0){
                return null;
            }
            return date;
        } catch (Exception e) {
            return null;
        }
    }
    
    
    public static boolean isEmail(String email) {
            String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
		+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
            Pattern pattern = Pattern.compile(EMAIL_PATTERN);
            Matcher matcher = pattern.matcher(email);
            return matcher.matches();
        
    }
    
    public  static User createUser(HttpServletRequest request,boolean checkPassword) throws Exception{
        User user=new User();
        String username=request.getParameter(User.USERNAME);
        if(!isValidString(username)){
            throw new RuntimeException(""+User.USERNAME);
        }else{
            user.setUsername(username);
        }
        String email=request.getParameter(User.EMAIL);
        String email_confirm=request.getParameter(User.EMAIL+"_confirm");
        if((!isEmail(email))||(!isEmail(email_confirm))||(!email.equals(email_confirm))){
            throw new RuntimeException(""+User.EMAIL);
        }else{
            user.setEmail(email);
        }
        if(checkPassword){
            String[] password=request.getParameterValues(User.PASSWORD);
            if((!isValidString(password[0]))||(!password[0].equals(password[1]))){
                throw new RuntimeException(""+User.PASSWORD);
            }else{
                String crypto=Crypto.crypto(password[0]);
                user.setPassword(crypto);
            }
        }
        String firstName=request.getParameter(User.FIRST_NAME);
        if(!isValidString(firstName)){
            throw new RuntimeException(""+User.FIRST_NAME);
        }else{
            user.setFirst_name(firstName);
        }
        String lastName=request.getParameter(User.LAST_NAME);
        if(!isValidString(lastName)){
            throw new Exception(""+User.LAST_NAME);
        }else{
            user.setLast_name(lastName);
        }
        String birthdateString=request.getParameter(User.BIRTHDATE);
        Date birthdate=dateFromString(birthdateString);
        if(birthdate==null){
            throw new Exception(""+User.BIRTHDATE);
        }else{
            user.setBirthdate(birthdate);
        }
        String address=request.getParameter(User.ADDRESS);
        if(!isValidString(address)){
            throw new Exception(""+User.ADDRESS);
        }else{
            user.setAddress(address);
        }
        return user;
    }
    
    private static boolean isValidString(String string){
        boolean valid=false;
        if(string!=null){
            int i=0;
            while((valid==false)&&(i<string.length())){
                if(string.charAt(i)==' '){
                    i++;
                }else{
                    valid=true;
                }
            }
        }
        return valid;
    }
    
}
